package com.xjscrm.console.chat.core.impl;

import cn.hutool.core.util.IdUtil;
import com.uzai.mobile.collect.api.MicaTopics;
import com.uzai.mobile.collect.api.dto.MicaTriggerDto;
import com.uzai.mobile.collect.api.dto.trigger.group.MicaTriggerGroupDelAdminMsg;
import com.uzai.mobile.collect.api.enums.MicaTriggerType;
import com.xjscrm.common.entity.WwDevice;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.enums.ChatTalkSessionType;
import com.xjscrm.console.chat.frame.recv.RGroupDelAdminMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.entity.ChatTalkSession;
import com.xjscrm.console.mq.producer.SendMsgProducer;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
import com.xjscrm.console.service.customer.wwDevice.WwDeviceService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/12 09:41
 */
@Component
@Slf4j
public class ChatGroupDelAdminMsgHandler extends ChatBaseMsgHandler<RGroupDelAdminMsg> {

    @Autowired
    private WwDeviceService deviceService;

    @Autowired
    private SendMsgProducer sendMsgProducer;

    @Autowired
    private ChatTalkSessionService chatTalkSessionService;

    @Override
    public SResultMsg doHandle(RGroupDelAdminMsg content, ChannelHandlerContext ctx) {

        try {
            Long merId = content.getMerId();
            Long deviceUniqueId = content.getDeviceUniqueId();

            WwDevice byId = deviceService.getById(merId, deviceUniqueId);
            if (Objects.isNull(byId)) {
                throw new BusinessException("机器人设备未找到");
            }

            ChatTalkSession byId1 = chatTalkSessionService.getById(merId, content.getTalkSessionId());
            if (Objects.isNull(byId1)) {
                throw new BusinessException("删除群管理员失败: 群会话不存在");
            }

            if (!Objects.equals(ChatTalkSessionType.group_conversation.getCode(), byId1.getType())) {
                throw new BusinessException("删除群管理员失败: 非群聊会话");
            }

            MicaTriggerGroupDelAdminMsg delAdminMsg = new MicaTriggerGroupDelAdminMsg();
            delAdminMsg.setGroupId(byId1.getConversationId());
            delAdminMsg.setWxIds(content.getWxIds());

            MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(delAdminMsg, MicaTriggerType.TRIGGER_GROUP_DEL_ADMIN, item -> {
                item.setReqId(IdUtil.getSnowflakeNextId());
                item.setDeviceId(byId.getDeviceId());
                item.setDeviceType(byId.getDeviceType());
            });
            sendMsgProducer.pushMsg(micaTriggerDto, MicaTopics.WW_TRIGGER_TOPIC);

            return SResultMsg.success(content, "删除群管理员进行中");
        } catch (Exception e) {
            log.error("删除群管理员失败", e);
            throw new BusinessException("删除群管理员失败");
        }
    }

    @Override
    public boolean checkMsg(RGroupDelAdminMsg baseMsg) {
        return super.checkMsg(baseMsg) || !Objects.nonNull(baseMsg.getDeviceUniqueId()) || Objects.isNull(baseMsg.getTalkSessionId()) || ObjectUtils.isEmpty(baseMsg.getWxIds());
    }

    @Override
    public Class<RGroupDelAdminMsg> msgClass() {
        return RGroupDelAdminMsg.class;
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.r_del_group_admin;
    }
}
